package com.ljkj.blockpay.presenter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.Request;
import com.bumptech.glide.request.target.SizeReadyCallback;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.JsonResponseListener;
import com.kongzue.baseokhttp.util.JsonBean;
import com.kongzue.baseokhttp.util.JsonList;
import com.kongzue.baseokhttp.util.JsonMap;
import com.kongzue.baseokhttp.util.Parameter;
import com.ljkj.blockpay.R;
import com.ljkj.blockpay.basedata.BasePresenterImpl;
import com.ljkj.blockpay.model.RechargeModel;
import com.ljkj.blockpay.network.ApiToCConfig;
import com.ljkj.blockpay.presenter.inter.MainInter;
import com.ljkj.blockpay.presenter.inter.ReceiptInter;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

public class ReceiptPresenter extends BasePresenterImpl<ReceiptInter.ReceiptInterView> implements ReceiptInter.ReceiptInterPresenter{

    public ReceiptPresenter(@NonNull Context context, @NonNull ReceiptInter.ReceiptInterView view) {
        super(context, view);
    }

    //收款码地址
    @Override
    public void qrcodeCollect(String amount,String currencyType) {
        JsonMap jsonMap = new JsonMap();
        jsonMap.put("amount",amount);
        jsonMap.put("currencyType",currencyType);
        HttpRequest.JSONPOST(getContext(), ApiToCConfig.URL_COLLECT, jsonMap, new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (main.getInt("code") == 0){
                    JsonMap jsondata = main.getJsonMap("data");
                    getView().qrcodeSuccess(jsondata);

                } else {

                }
            }
        });
    }
    @Override
    public void getNetAccuracy() {
        HttpRequest.GET(getContext(), ApiToCConfig.URL_TRADE_NETWORK_ACCURACY, new Parameter().add("orderType", "4"), new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (main.getInt("code") == 0){
                    JsonList jsonList = main.getList("data");
                    List<RechargeModel> list = new ArrayList<>();
                    for (int i = 0; i < jsonList.size(); i++) {
                        list.add(JsonBean.getBean(jsonList.getJsonMap(i), RechargeModel.class));
                    }
                    getView().showNetAccuracy(list);
                    Log.d("zdl","==onSafeSuccess==="+list.size());
                }else{
                }
            }
        });
    }

    @Override
    public void getColllist() {
        HttpRequest.GET(getContext(), ApiToCConfig.URL_COLLECTLIST, new Parameter(), new JsonResponseListener() {
            @Override
            public void onResponse(JsonMap main, Exception error) {
                if (main.getInt("code") == 0){
                    JsonList mainList = main.getList("data");
                    getView().collList(mainList);

                } else {

                }
            }
        });
    }
    private  Bitmap drawableToBitmap(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(0xff33B5E5);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);
        return bitmap;
    }
    public static Bitmap drawable2Bitmap(Drawable drawable){
        if(drawable instanceof BitmapDrawable){
            return ((BitmapDrawable)drawable).getBitmap() ;
        }else if(drawable instanceof NinePatchDrawable){
            Bitmap bitmap = Bitmap
                    .createBitmap(
                            drawable.getIntrinsicWidth(),
                            drawable.getIntrinsicHeight(),
                            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                                    : Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight());
            drawable.draw(canvas);
            return bitmap;
        }else{
            return null ;
        }
    }

    public void getBitmap2(String url){
        Glide.with(getContext()).load(url).into(new Target<Drawable>() {
            @Override
            public void onLoadStarted(@Nullable Drawable placeholder) {
                getView().getBitmap(drawable2Bitmap(placeholder));
            }

            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {

            }

            @Override
            public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {

            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {

            }

            @Override
            public void getSize(@NonNull SizeReadyCallback cb) {

            }

            @Override
            public void removeCallback(@NonNull SizeReadyCallback cb) {

            }

            @Override
            public void setRequest(@Nullable Request request) {

            }

            @Nullable
            @Override
            public Request getRequest() {
                return null;
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {

            }

            @Override
            public void onDestroy() {

            }
        });
    }

}
